/*
 * Fichier source C++ du projet menulib
 *
 * Cible : PSP
 * Auteur : BuLLeKeUp
 * Date de création : 17/03/2012
 * Date de dernière modification : 17/03/2012
 *
 * Ce projet est sous license GNU GPL v3. Consultez la license associée dans le repertoire doc/gplv3.txt
 * ou rendez vous sur http://www.gnu.org/copyleft/gpl.html
 *
 * Nom du fichier : callbacks.cpp
 *
 * Description : Fichier d'implémentation des fonctions de callback pour le programme
 *
 */

#include "callbacks.h"
#include "defines.h"

//This function will be used when the callbacks from the main thread is going to be call. Will exit the game properly.
int exitGame(int arg1, int arg2, void *common){
    sceKernelExitGame();
    return EXIT_SUCCESS;
}

//This function sets up a callback, then waits it to be called from the thread you run it. Finally exits the game with the exitGame() function
int setupExitCallback(SceSize args, void *argp){
    int callbackID;
    
    //Creates a callback for the thread which will use exitGameFunction as callback function
    callbackID = sceKernelCreateCallback("Exit Callback", exitGame, NULL);
    sceKernelRegisterExitCallback(callbackID);
    
    //Take the thread in sleep mode but look for associate callbacks
    sceKernelSleepThreadCB();
    return EXIT_SUCCESS;
}

//This function sets up and starts a thread which just creates the exit callback and wait. Return the ID of the thread
int setupCallbackThread(){
    SceUID threadID;
    
    threadID = sceKernelCreateThread("Callback update thread", setupExitCallback, 0x11, 0xFA0, THREAD_ATTR_USER, 0);
    if(threadID > 0)
        sceKernelStartThread(threadID, 0, NULL);
    return threadID;
}